<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>百度离线版2.0DEMO</title>
<script type="text/javascript" src="js/apiv2.0.3.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/bmap.css"/>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/findtiles.js"></script>
<script type="text/javascript" src="js/area.js"></script>
</head>
<body>
		
	<!-- 11 3000 3000
	12 5000 5000
	13 9000 9000 -->
<div style="width:3000px;height:3000px;border:1px solid gray" id="container"></div>
<br>
<input type="button" value="findtiles" onclick="findtiles();">
<!-- //<input type="button" value="downImg" onclick="clickDown()"> -->
<br>
<div id="list"></div>
</body>
</html>

<script type="text/javascript">
/*城市*/
//城市 左下角                右上角                 中心点
//北京 115.463049,39.657865  117.029202,40.541792   116.405335,39.917787          
//上海 120.862372 30.676443  122.006022 31.8733     121.47709,31.234694
//武汉 113.707018 29.972313  115.087233  31.367044  114.299513,30.601202 湖北 108.374308  29.035728    116.143307 33.277091    114.310499,30.608559
//黑龙江 121.189549 43.432678   135.103684  53.568958   126.649534,45.783138
//重庆 105.296455 28.166366  110.206934 32.207209    106.554988,29.570253
//安徽 114.889119 29.399579  119.655494 34.65681     117.264424,31.843069
//福建 115.858543 23.564397  120.440069 28.3112      119.312795,26.079084
//甘肃 92.32886  92.32886    108.721607 42.801545    103.837649,36.067566
//广东 109.636498 20.065795  117.365721 25.522642    113.265527,23.145127
//广西 104.456761 20.874472  112.068332  26.391498
//贵州 103.608978 24.624434  109.566131 29.226751
//海南 108.578953 18.121885  111.323765  20.254476
//河北 113.468897 36.055732  119.869576  42.623632
//河南 110.366453 31.388635  116.656735  36.372803
//湖南 108.797402 24.642208     114.267743  30.132388
//江苏 116.36856  30.764191     122.005714  35.128722
//江西 113.58273  24.491938     118.493266  30.080927
//吉林 121.648978 40.867574     131.325647  46.311072
//辽宁  118.852571 38.684456    125.797304  43.498162
//天津 116.71421 38.561403   118.074648 40.259068   117.198025,39.09882
    //北京 115.463049,39.657865 117.029202,40.541792                 116.405335,39.917787
    //上海 左下角：120.862372 30.676443 右上角：122.006022 31.8733    121.47709,31.234694
    //天津 116.71421 38.561403      118.074648 40.259068             117.198025,39.09882
var map = new BMap.Map("container",{mapType: BMAP_NORMAL_MAP,maxZoom:14,minZoom:11});      //设置卫星图为底图
var point = new BMap.Point(115.875508,28.684892);    // 创建点坐标
map.centerAndZoom(point,11);                     // 初始化地图,设置中心点坐标和地图级别。

//map.addControl(new BMap.MapTypeControl());
map.addControl(new BMap.NavigationControl());
//map.enableScrollWheelZoom(false);                  // 启用滚轮放大缩小。
map.enableKeyboard();                         // 启用键盘操作。  
//map.setCurrentCity("北京");          // 设置地图显示的城市 此项是必须设置的
//map.setCurrentCity('石家庄')
var b = new BMap.Bounds(new BMap.Point(115.445118,28.163177),new BMap.Point(116.572049,29.179678)); // 范围 左下角，右上角的点位置 
try {    // js中尽然还有try catch方法，可以避免bug引起的错误
	BMapLib.AreaRestriction.setBounds(map, b); // 已map为中心，已b为范围的地图
} catch (e) {
	// 捕获错误异常
	alert(e);
}
//北京 116.405335,39.917787 var b = new BMap.Bounds(new BMap.Point(115.463049,39.657865),new BMap.Point(117.029202,40.541792)); // 范围 左下角，右上角的点位置 

// var marker = new BMap.Marker(point);
// map.addOverlay(marker); 

// var polyline = new BMap.Polyline([
// 		new BMap.Point(111.404, 40.915),
// 		new BMap.Point(112.404, 42.915),
// 		new BMap.Point(113.404, 39.915),
// 		new BMap.Point(114.404, 42.915),
// 		new BMap.Point(115.404, 39.915),
// 		new BMap.Point(116.404, 42.915)
// 	], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5});
// map.addOverlay(polyline);


function addUrls(path, url){
    var div = document.getElementById("list");
    var anchor = document.createElement("a");
    anchor.href = url;
    anchor.innerHTML = path;
    div.appendChild(anchor);
    var br = document.createElement("br");
    div.appendChild(br);
	anchor.download=path;

	getBase64(url)
            .then(function(base64){
                  console.log(base64);//处理成功打印在控制台
            },function(err){
                  console.log(err);//打印异常信息
            });
    //调用创建iframe的函数
	//mm1(url,name) 	
}



//传入图片路径，返回base64
function getBase64(img){
	//var name= img.
	function getBase64Image(img,width,height) {//width、height调用时传入具体像素值，控制大小 ,不传则默认图像大小

	  var canvas = document.createElement("canvas");
	  canvas.width = width ? width : img.width;
	  canvas.height = height ? height : img.height;

	  var ctx = canvas.getContext("2d");
	  ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
	  var dataURL = canvas.toDataURL();
	  //return dataURL;
		var alink = document.createElement("a");
		alink.href = dataURL;
		alink.download =img.src.split('?')[1].split('styles')[0];
		alink.click(); 
	}
	var image = new Image();
	image.crossOrigin = '';
	image.src = img;
	var deferred=$.Deferred();
	if(img){
	  image.onload =function (){
		deferred.resolve(getBase64Image(image,name));//将base64传给done上传处理
	  }
	  return deferred.promise();//问题要让onload完成后再return sessionStorage['imgTest']
	}
  }


// function mm1(url,name)   
// {   
// 	var alink = document.createElement("a");
// 	alink.href = url;
// 	alink.download = name;
// 	alink.click(); 
// }   
function findtiles(){
	var findAllTiles = downloadBaiduTiles();
	findAllTiles(map, addUrls);
}

</script>
